from django.db.models.fields.reverse_related import ManyToOneRel, ManyToManyRel, OneToOneRel

# 获取查询时排序规则
def get_order_list_combined(data):
    order_list_combined = []
    prop_list = data.get('sort_prop', '').split(
        ',') if data.get('sort_prop') else []
    order_list = data.get('sort_order', '').split(
        ',') if data.get('sort_order') else []
    if len(prop_list) != len(order_list):
        return []
    for i in range(len(prop_list)):
        for prop, order in zip(prop_list, order_list):
            if order.lower() == 'ascending':
                order_list_combined.append(prop)
            elif order.lower() == 'descending':
                order_list_combined.append(f'-{prop}')
    return order_list_combined

# 获取模型序列化器配置
def get_model_serializer_config(serializer):
    config_data = []
    serializer_model = serializer.Meta.model
    model_fields = [field.name for field in serializer_model._meta.get_fields(
    ) if not isinstance(field, (ManyToOneRel, ManyToManyRel, OneToOneRel))]
    if hasattr(serializer.Meta, "fields"):
        serializer_fields = serializer.Meta.fields
    elif hasattr(serializer.Meta, "exclude"):
        serializer_fields = [
            field for field in model_fields if field not in serializer.Meta.exclude]
    for field_name in serializer_fields:
        if field_name in model_fields:
            field = serializer_model._meta.get_field(field_name)
            field_info = {
                "title": field.verbose_name,
                "dataIndex": field.name,
                "type": field.get_internal_type()
            }
            if not field.blank:
                field_info["required"] = True
            config_data.append(field_info)
        else:
            config_data.append({
                "title": None,
                "dataIndex": field_name,
                "type": None
            })
    return config_data